package Threads.ThreadPool.java_threadpool;


import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* shutdown()
线程池状态变为 SHUTDOWN
- 不会接收新任务
- 但已提交任务会执行完
- 此方法不会阻塞调用线程的执行
*/

/*shutdownNow()
线程池状态变为 STOP
- 不会接收新任务
- 会将队列中的任务返回
- 并用 interrupt 的方式中断正在执行的任务
*/
public class shutdown_test {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS,new LinkedBlockingDeque<Runnable>());
        System.out.println(threadPoolExecutor);
        threadPoolExecutor.execute(()->{
            try {
                System.out.println("running---1");
                Thread.sleep(1000);
                System.out.println("Done---1!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        threadPoolExecutor.execute(()->{
            try {
                System.out.println("running---2");
                Thread.sleep(1000);
                System.out.println("Done---2!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        //threadPoolExecutor.shutdown();
        List<Runnable> runnables = threadPoolExecutor.shutdownNow();
        System.out.println(runnables.size());
        //shutdown之后不会接收新的任务了，会报RejectedExecutionException
        threadPoolExecutor.execute(()->{
            System.out.println("3 running");
        });
    }
}
